package com.foreknow.demo6;

public class Upcasting {
    public static void main(String[] args) {
        Cat c= new Cat();
        //向上转型
        Animal a= new Cat();
        //向下转型
        Cat c2 = (Cat)a;

        Animal animal =  buyAnimal();

        if(animal instanceof Cat){
            Cat cat = (Cat)animal;
            cat.miao();
        }
        if (animal instanceof Dog){
            Dog dog = (Dog)animal;
            dog.bark();
        }
    }
    public static Animal buyAnimal(){
        return new Cat();
    }
}
class Animal {
    String name;
    public void eat(){

    }
}
class Cat extends Animal{
    int age;
    public void miao(){

    }
}
class Dog extends Animal{
    String bread;
    int age;
    public void bark(){}
}